﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.APP_UI.Common;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_ExpenseGroup : System.Web.UI.Page
    {
        IExpenseGroupBLL expGrpCtrl = new ExpenseGroupBLL();
        private RadComboBox sourceCombo, parentCombo;
        private CommandControl cmdFac = new CommandControl();
        private Main main = new Main();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                cbExpenseGroup.LoadExpenseGroupComboBox();
            }
            
        }

        private void LoadDataGrid()
        {
            LoadDataGrid(0);
        }

        private void LoadDataGrid(int seq)
        {
            IList<TreeViewInfo> treeView = new List<TreeViewInfo>();

            Grid.DataSource = expGrpCtrl.Search(seq);
            Grid.DataBind();

            //tView.DataSource = expGrpCtrl.TreeView(treeView, false);
            //tView.DataBind();

            //tView.ExpandAllNodes();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            parentCombo = (RadComboBox)cbExpenseGroup.FindControl("cbExpenseGroup");

            CmdCtrl.Inserted += new EventHandler(OnInsert);
            CmdCtrl.Updated += new EventHandler(OnUpdate);
            CmdCtrl.Deleted += new EventHandler(OnDelete);
        }
        #endregion

        private void OnInsert(object sender, EventArgs e)
        {
            bool isDuplicated = expGrpCtrl.CheckDuplicateCode(txtCode.Text);

            if (isDuplicated == false)
            {
                string str = txtCode.Text.Trim();
                Double Num;
                bool isNum = double.TryParse(str, out Num);
                if (isNum == false)
                {
                    main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                }
                else
                {

                    int parent = (parentCombo.SelectedIndex > 0) ? Convert.ToInt32(parentCombo.SelectedValue) : 0;
                    //int source = (sourceCombo.SelectedIndex > 0) ? Convert.ToInt32(sourceCombo.SelectedValue) : 0;
                    int source = 0;

                    expGrpCtrl.Insert(txtCode.Text, txtName.Text, txtDescr.Text, parent, source, txtGFMISCode.Text);
                    LoadDataGrid();
                    CmdCtrl.RefreshGrid();
                }
            }
            else 
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
            }
        }

        private void OnUpdate(object sender, EventArgs e)
        {
            bool isDuplicated = expGrpCtrl.CheckDuplicateCode(txtCode.Text);

            if (isDuplicated == false)
            {
                string str = txtCode.Text.Trim();
                Double Num;
                bool isNum = double.TryParse(str, out Num);
                if (isNum == false)
                {
                    main.MessageAlert(Page, "กรอกเป็นตัวเลขเท่านั้น");
                }
                else
                {
                    int parent = (parentCombo.SelectedIndex > 0) ? Convert.ToInt32(parentCombo.SelectedValue) : 0;
                    //int source = (sourceCombo.SelectedIndex > 0) ? Convert.ToInt32(sourceCombo.SelectedValue) : 0;
                    int source = 0;

                    expGrpCtrl.Update(Convert.ToInt32(hdSeq.Text), txtCode.Text, txtName.Text, txtDescr.Text, parent, source, txtGFMISCode.Text);

                    LoadDataGrid();
                    CmdCtrl.MapControl();
                }
            }
            else
            {
                main.MessageAlert(Page, "รหัสซ้ำ");
            }
        }

        private void OnDelete(object sender, EventArgs e)
        {
            int nRowSelected = 0;

            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    expGrpCtrl.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]));

                    nRowSelected++;
                }
            }
            else
            {
                expGrpCtrl.Delete(Convert.ToInt32(hdSeq.Text));
            }

            LoadDataGrid();
            CmdCtrl.RefreshGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetClick")
            {
                IList<ExpenseGroupInfo> expGrpList = expGrpCtrl.Search(Convert.ToInt32(((Label)e.Item.FindControl("targetSequence")).Text));

                if (expGrpList.Count > 0)
                {
                    hdSeq.Text = expGrpList[0].Sequence.ToString();
                    txtCode.Text = expGrpList[0].Code;
                    txtName.Text = expGrpList[0].Name;
                    txtDescr.Text = expGrpList[0].Description;
                    txtGFMISCode.Text = expGrpList[0].Gfmis_code;
                    //sourceCombo.SelectedValue = expGrpList[0].Source.Sequence.ToString();
                    //cbExpenseGroup.LoadExpenseGroupComboBox(0, "", "", "", 0, expGrpList[0].Source.Sequence);
                    
                    if (expGrpList[0].Parent != null)
                        parentCombo.SelectedValue = expGrpList[0].Parent.Sequence.ToString();

                    CmdCtrl.MapControl();
                }
            }
            else
            {
                LoadDataGrid();
                CmdCtrl.RefreshGrid();
            }
        }

        protected void tView_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            string[] type = e.Node.Value.Split('-');

            if (type[0].ToString() == "EXPENSEGROUP")
            {
                Grid.DataSource = expGrpCtrl.Search(0, "", "", "", Convert.ToInt32(type[1].ToString()), 0);
            }

            Grid.DataBind(); 
            cmdFac.GridCommand(panelList, panelGrid, CmdCtrl);
        }

    }
}
